package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Visitors;
import com.tanhua.dubbo.api.mongo.VisitorsApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class VisitorsApiImpl implements VisitorsApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Visitors> queryVisitors(Long userId, Long lastTime, Integer i) {
        Query query = null;
        if(lastTime!=null){
            query = new Query(Criteria.where("userId").is(userId).and("date").gt(lastTime));
        }else{
            query = new Query(Criteria.where("userId").is(userId));
        }
        query.with(Sort.by(Sort.Order.desc("date"))).limit(i);
        List<Visitors> visitorsList = mongoTemplate.find(query, Visitors.class);
        return visitorsList;
    }
}
